using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace ImportModelFromRhino
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Camera : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public Matrix world;
        public Matrix view, viewRotation;
        public Matrix projection;

        float yaw, pitch, roll, speed = 0.1f;
        Vector3 pos, target;

        public Camera(Game game, Vector3 pos, Vector3 target, Matrix projection)
            : base(game)
        {
            this.pos = pos;
            this.target = target;
            this.projection = projection;
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            // TODO: Add your initialization code here
            world = Matrix.Identity;
            view = Matrix.CreateLookAt(pos, target, Vector3.Up);

        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                yaw += speed / 2;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                yaw -= speed / 2;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                pitch -= speed / 2;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                pitch += speed / 2;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                pos += viewRotation.Forward * speed * 20.0f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
                pos += viewRotation.Backward * speed * 20.0f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.A))
            {
                pos += viewRotation.Left * speed * 20.0f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.D))
            {
                pos += viewRotation.Right * speed * 20.0f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Q))
            {
                roll += speed;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.E))
            {
                roll -= speed;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                pos += viewRotation.Up * speed * 20.0f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.LeftControl))
            {
                pos += viewRotation.Down * speed * 20.0f;
            }

            //viewRotation = Matrix.CreateRotationX(pitch) *
            //            Matrix.CreateRotationY(yaw) *
            //            Matrix.CreateRotationZ(roll);

            viewRotation = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll);

            //view = Matrix.CreateTranslation(pos) *
            //       Matrix.Transpose(viewRotation);

            view = Matrix.CreateLookAt(pos, viewRotation.Forward + pos, viewRotation.Up);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {

        }

    }
}
